import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "Login_Servlet",urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String[] logins = request.getParameterValues("login");
        if(logins != null) {
            for (String login : logins) {
                if("账户登录".equals(login)){
                    String username = request.getParameter("username");
                    String password = request.getParameter("password");
                    if("liyibo".equals(username)&&"18210220212".equals(password)){
                        //登录成功
                        request.getSession().setAttribute("username",username);
                        response.sendRedirect(request.getContextPath()+"/chatRoom.jsp");
                    }else {
                        //登录失败  login登录页输出错误
                        request.setAttribute("Ext","账户或密码错误，重新登录！");
                        request.getRequestDispatcher("/login.jsp").forward(request,response);
                    }
                }else if("免账户登录".equals(login)){
                    String username = request.getParameter("username");
                    request.getSession().setAttribute("username",username);
                    response.sendRedirect(request.getContextPath()+"/chatRoom.jsp");
                }

            }
        }


    }
}
